package com.sunshine.exception;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(RuntimeException.class)
    public ResponseEntity<String> handleRuntimeException(RuntimeException e) {
        // 添加对getMessage()返回值的null检查，避免NullPointerException
        if (e.getMessage() != null && e.getMessage().contains("账户余额不足")) {
            return ResponseEntity.badRequest().body("支付失败，账户余额不足");
        }
        return ResponseEntity.badRequest().body("支付失败: " + (e.getMessage() != null ? e.getMessage() : "未知错误"));
    }
}